<?php

/**
 * setting actions.
 *
 * @package    berta-s
 * @subpackage setting
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class settingActions extends sfActions{
    /* @todo: smells like overengineering */
	public function preExecute(){
        $request=$this->getRequest();
		if ($request->getParameter("setting_type")=="template"){
            if ($request->hasParameter("id")){
                $template_setting=Doctrine::getTable("TemplateSetting")->find($request->getParameter("id"));
                $this->setting=$template_setting;

                //$this->setting=$this->getRoute()->getObject();
			    $this->settings=TemplateSettings::getInstance($this->setting);
            }else{
                $this->settings=TemplateSettings::getInstance();
                $this->setting=$this->settings->getSettingModel();
            }
			$this->form=new TemplateSettingForm($this->setting);
		}else{
			$this->settings=Settings::getInstance();
			$this->setting=$this->settings->getSettingModel();
			$this->form=new SettingForm($this->setting);
		}
	}

	/* @TODO: DRY up. duplicate with entry/image/section! */
	/* @TODO: remove incoming data container ("setting") */
	private function saveSettingsFromRequest(sfWebRequest $request){
		if ($request->hasParameter("setting") || $request->getFiles("setting")!="") {
			$inputParams=$request->getParameter("setting");

			$inputParams["id"]=$this->form->getObject()->getId();

			$useFields=array();
			if (is_array($inputParams)){
				foreach ($inputParams as $name => $param){
					$useFields[]=$name;
				}
			}
			if (is_array($request->getFiles("setting"))){
				foreach ($request->getFiles("setting") as $name => $param){
					$useFields[]=$name;
				}
			}

			if (sizeof($useFields)>0){
				$this->form->useFields($useFields);
			}

			$this->form->bind($inputParams, $request->getFiles("setting"));
			if ($this->form->isValid()) {
				$this->form->save();
				$values=array();
//				$values["id"]=$this->form->getObject()->getId();
				foreach ($inputParams as $key => $value){
					if ($key!="id" && $key!="_csrf_token"){
						$values[$key]=$this->form->getValue($key);
					}
				}
                foreach ($request->getFiles("setting") as $key => $value){
                    if ($this->form->getValue($key) instanceOf sfValidatedFile){
                        $values[$key]=PathHelper::getPublicPath($this->form->getValue($key)->getSavedName());
                    }
                }

				$this->values=$values;

				$this->setTemplate("XMLHttp", "global");
			}else{
				$this->setTemplate("XMLHttpError", "global");
			}
		}else{
			$this->setTemplate("XMLHttp");
		}
	}

	public function executeIndex(sfWebRequest $request){
		if ($request->hasParameter("filter")){
			$filter=$request->getParameter("filter");
			if ($filter=="values"){
				$this->filter=$filter;
			}else if ($filter=="definitions"){
				$this->filter=$filter;
			}
		}
	}

	public function executeUpdate(sfWebRequest $request){
		$this->saveSettingsFromRequest($request);
	}

}



